--[[
Name: BrownieHelper
Developed by: Sonora (The Dragon Flight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

function ViewEventsFrame:Initialize(app)
    
    --Save the app
    self.app = app
    
    --Register for events
    self:SetScript("OnShow", function() self:OnShow() end)
    self:SetScript("OnHide", function() self:OnHide() end)
    
    --Setup the table
    ViewEventsTable:setWidths({65,115,70,175,180})
    ViewEventsTable:setFields({"Time","Event","Player","Item","Description"})
    ViewEventsTable:registerRowEnterCallback(function(row) self:rowEnter(row) end)
    ViewEventsTable:registerRowLeaveCallback(function(row) self:rowLeave(row) end)
    
    --Register for updates
    self.app:registerEvent("BrownieHelper_EventsChanged", function() self:updateDewTab();self:updateView(); end)
    self.app:registerEvent("BrownieHelper_ModeChanged", function(mode) self:OnModeChanged(mode) end)
    
    --Create a dew drop table
    self.addEventDewDrop = AceLibrary("Dewdrop-2.0")
    
    --Start our dew drop table
    self.eventDewTab = {
        handler=self,
        type = "group",
        args = {
            h1 = {
                type="header",
                name="Available events:",
                order=1,
            },
            h2 = {
                type = "header",
                order=100,
            },
            close = {
                type="execute",
                name="Close",
                desc="Close this menu.",
                func= function() self.addEventDewDrop:Close() end,
                order=1000,
            },
        },
    }
    self:updateDewTab()
    
    --Call On show
    self:updateView()
    
end

function ViewEventsFrame:updateDewTab()
    --Update the connads on the dew table
    for i,event_name in pairs(self.app:getRewardEvents()) do
        self.eventDewTab.args[i] = {
            type = 'execute',
            name = event_name,
            desc = 'Record the '..event_name..' event along with the character roster and time in the current run database.\n\nPoint value:  '..tostring(self.app:getEventReward(event_name)),
            func = function() self.app:addEvent(event_name); self.addEventDewDrop:Close(); end,
            disabled = function() return self.app:getMode()<BROWNIEHELPER_MASTERLOOTER end,
            order = i+10,
        }
    end
    
    --make sure its cleared out?
    for i=#self.app:getRewardEvents()+1,25 do
        self.eventDewTab.args[i] = nil
    end
    
end

function ViewEventsFrame:OnModeChanged(mode)
    if mode>=BROWNIEHELPER_MASTERLOOTER and self.app:getCurrentRun()~=nil then
        ViewEventsAddEventButton:Enable()
        ViewEventsRemoveEventButton:Enable()
        self:OnHide()
    else
        ViewEventsAddEventButton:Disable()
        ViewEventsRemoveEventButton:Disable()
    end
end

function ViewEventsFrame:OnShow()
    local mode = self.app:getMode()
    if mode<BROWNIEHELPER_MASTERLOOTER and mode>BROWNIEHELPER_QUIESCENT then
        self.app:registerForCommand(self.app:getMasterLooter(),"GetEventUpdates")
    end
end
function ViewEventsFrame:OnHide()
    local mode = self.app:getMode()
    if mode<BROWNIEHELPER_MASTERLOOTER and mode>BROWNIEHELPER_QUIESCENT then
        self.app:unregisterForCommand(self.app:getMasterLooter(),"GetEventUpdates")
    end
end

function ViewEventsFrame:updateView()
   
    --clear out the rows
    ViewEventsTable:clearRows()
    
    --Setup the events table
    for eventName, eventInfo in pairs(self.app:getEventsDb()) do
        ViewEventsTable:addRow({
            --Shown columns
            Time=(eventInfo.gametime or ""),
            Event=(eventInfo.event or ""),
            Player=(eventInfo.player or (eventInfo.players or "")),
            Item=(eventInfo.itemlink or ""),
            Description=(eventInfo.description or ""),
            
            --Internal event obj
            eventInfo = eventInfo,
            eventName = eventName,
            wiTableSelected = eventInfo.checked,
        })
    end
    
    ViewEventsRunLabel:SetText(self.app:getCurrentRun())
    
    --update our buttons
    self:OnModeChanged(self.app:getMode())
    
end

function ViewEventsFrame:OnAddEvent()
    self.addEventDewDrop:Open(ViewEventsAddEventButton, 'children', function() self.addEventDewDrop:FeedAceOptionsTable(self.eventDewTab) end)
end

function ViewEventsFrame:OnDeleteEvent()
    local deleteRow = ViewEventsTable:getSelections()[1]
    if deleteRow then
        yesnoDialog("Are you sure you want to delete event:\n\n"..deleteRow.eventName,
            function(response)
                if response==true then self.app:deleteEvent(deleteRow.eventName) end
            end
        )
    end
end

function ViewEventsFrame:OnClose() self:Hide() end

function ViewEventsFrame:rowEnter(row)

    local eventInfo = row.eventInfo
    local eventName = row.eventName

    --position the game tool tip
    GameTooltip_SetDefaultAnchor( GameTooltip, ViewEventsFrame )
    GameTooltip:ClearAllPoints()
    GameTooltip:SetPoint("TOPLEFT", ViewEventsFrame, "TOPRIGHT", 0, -15)
    
    --fill in the defined keys
    GameTooltip:AddLine(eventName, HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b)
    
    for k,v in pairsByKeys(eventInfo) do
        if type(k)=="string" then
            GameTooltip:AddLine("\n"..k, HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b, false)
            GameTooltip:AddLine(tostring(v), NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, true)
        end
    end
    
    GameTooltip:Show()
end

function ViewEventsFrame:rowLeave(row)
    GameTooltip:Hide()
end



